﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.ComponentModel;
using System.Windows.Controls;
using System.Diagnostics;

namespace Rsa
{
    public class LancarThreads
    {
        public delegate void EmailHandler(List<Lista> listaEmail);
        public event EmailHandler emailEvento;

        public delegate void TwitterHandler(List<Lista> listaTwitter);
        public event TwitterHandler twitterEvento;

        public delegate void MoodleHandler(List<Lista> listaMoodle);
        public event MoodleHandler moodleEvento;

        public delegate void EmentaHandler(List<Lista> listaEmenta);
        public event EmentaHandler ementaEvento;

        public delegate void FacebookHandler(List<Lista> listaFacebook);
        public event FacebookHandler facebookEvento;

        private int _buscarMoodle = 0;
        private int _buscarEmail = 0;

        private GestorConfigXML Gxml = null;

        private GestorTwitter Twitter = null;
        private GestorMoodle Moodle = null;
        private GestorEmail Email = null;
        private GestorEmenta Ementa = null;
        private GestorFacebook Facebook = null;



        public LancarThreads(GestorConfigXML gx)
        {
            Gxml = gx;
            Moodle = new GestorMoodle(gx);
            Email = new GestorEmail(gx);
            Twitter = new GestorTwitter(gx);
            Ementa = new GestorEmenta(gx);
            Facebook = new GestorFacebook(gx);
        }


        public bool startAll()
        {
            //Lancar Thread Moodle
            //this.startMoodle();

            return true;
        }


        #region twitter
        public void startTwitter(int opcao)
        {
            BackgroundWorker bgTwitter = new BackgroundWorker();
            bgTwitter.DoWork += new DoWorkEventHandler(twitter_DoWork);
            bgTwitter.RunWorkerCompleted += new RunWorkerCompletedEventHandler(twitter_RunWorkerCompleted);
            bgTwitter.RunWorkerAsync(opcao);
        }

        private void twitter_DoWork(object sender, DoWorkEventArgs e)
        {
            Console.WriteLine("Thread Twitter a trabalhar: " + Thread.CurrentThread.ManagedThreadId);
           this.Twitter.Dados((int)e.Argument);

        }

        private void twitter_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            foreach (Lista l in Twitter.ListaTwitter)
                l.Tipo = Lista.TWITTER;
            if (twitterEvento != null)
                twitterEvento(Twitter.ListaTwitter);
        }



        #endregion

        #region email

        //enviar emails
        public void sendEmail(string to, string subject, string body)
        {
            Email.To = to;
            Email.Subject = subject;
            Email.Body = body;
            BackgroundWorker bgSEndEmail = new BackgroundWorker();
            bgSEndEmail.DoWork += new DoWorkEventHandler(sendEmail_DoWork);
            bgSEndEmail.RunWorkerCompleted += new RunWorkerCompletedEventHandler(sendEmail_RunWorkerCompleted);
            bgSEndEmail.RunWorkerAsync();

        }
        private void sendEmail_DoWork(object sender, DoWorkEventArgs e)
        {
            Console.WriteLine("Thread Enviar Email a trabalhar: " + Thread.CurrentThread.ManagedThreadId);
            Email.sendEmail();
        }
        private void sendEmail_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

           /* if (emailEvento != null)
                emailEvento(Email.ListaEmail); */
        }


        //Receber Emails
        //Metodo que inicia o processo
        public void startEmail(int opcao)
        {
            BackgroundWorker bgEmail = new BackgroundWorker();
            bgEmail.DoWork += new DoWorkEventHandler(email_DoWork);
            bgEmail.RunWorkerCompleted += new RunWorkerCompletedEventHandler(email_RunWorkerCompleted);
            bgEmail.RunWorkerAsync(opcao);
        }

        private void email_DoWork(object sender, DoWorkEventArgs e)
        {
            Console.WriteLine("Thread Email a trabalhar: " + Thread.CurrentThread.ManagedThreadId);
            this.Email.start((int)e.Argument);

        }
        private void email_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (Email.ListaEmail != null)
                foreach (Lista l in Email.ListaEmail)
                    l.Tipo = Lista.EMAIL;


            if (emailEvento != null)
                emailEvento(Email.ListaEmail);
        }
        #endregion

        #region moodle
        public void startMoodle(int opcao)
        {
            BackgroundWorker bgMoodle = new BackgroundWorker();
            bgMoodle.DoWork += new DoWorkEventHandler(moodle_DoWork);
            bgMoodle.RunWorkerCompleted += new RunWorkerCompletedEventHandler(moodle_RunWorkerCompleted);
            bgMoodle.RunWorkerAsync(opcao);
        }

        private void moodle_DoWork(object sender, DoWorkEventArgs e)
        {
            Console.WriteLine("Thread Moodle a trabalhar: " + Thread.CurrentThread.ManagedThreadId);
            this.Moodle.start((int)e.Argument);
        }


        private void moodle_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (Moodle.ListaMoodle != null)
            {
                foreach (Lista l in Moodle.ListaMoodle)
                    l.Tipo = Lista.MOODLE;
            }

            if (moodleEvento != null)
                moodleEvento(Moodle.ListaMoodle);
        }

        #endregion

        #region ementa
        public void startEmenta(int opcao)
        {
            BackgroundWorker bgEmenta = new BackgroundWorker();
            bgEmenta.DoWork += new DoWorkEventHandler(ementa_DoWork);
            bgEmenta.RunWorkerCompleted += new RunWorkerCompletedEventHandler(ementa_RunWorkerCompleted);
            bgEmenta.RunWorkerAsync(opcao);
        }

        private void ementa_DoWork(object sender, DoWorkEventArgs e)
        {
            //Console.WriteLine("Thread Ementa a trabalhar: " + Thread.CurrentThread.ManagedThreadId);
            this.Ementa.start((int)e.Argument);
        }


        private void ementa_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (Ementa.ListaEmenta != null)
            {
                foreach (Lista l in Ementa.ListaEmenta)
                    l.Tipo = Lista.EMENTA;
            }

            if (ementaEvento != null)
                ementaEvento(Ementa.ListaEmenta);
        }
        #endregion

        #region facebook
        public void startFacebook(int opcao)
        {
            BackgroundWorker bgFacebook = new BackgroundWorker();
            bgFacebook.DoWork += new DoWorkEventHandler(facebook_DoWork);
            bgFacebook.RunWorkerCompleted += new RunWorkerCompletedEventHandler(facebook_RunWorkerCompleted);
            bgFacebook.RunWorkerAsync(opcao);
        }

        private void facebook_DoWork(object sender, DoWorkEventArgs e)
        {
            Console.WriteLine("Thread Facebook a trabalhar: " + Thread.CurrentThread.ManagedThreadId);
            this.Facebook.Dados((int)e.Argument);
        }


        private void facebook_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
          
                foreach (Lista l in Facebook.ListaFacebook)
                    l.Tipo = Lista.FACEBOOK;
    

            if (facebookEvento != null)
                facebookEvento(Facebook.ListaFacebook);
        }
        #endregion


        #region gets e sets

        public int BuscarEmail
        {
            get { return _buscarEmail; }
            set { _buscarEmail = value; }
        }

        public int BuscarMoodle
        {
            get { return _buscarMoodle; }
            set { _buscarMoodle = value; }
        }

        #endregion

    }
}
